package com.equipmentRent.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.equipmentRent.system.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.equipmentRent.system.mapper.OrderDetailMapper;
import com.equipmentRent.system.domain.OrderDetail;
import com.equipmentRent.system.service.IOrderDetailService;
import com.equipmentRent.common.core.text.Convert;

/**
 * 订单明细Service业务层处理
 *
 * @author zxl
 * @date 2025-05-17
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询订单明细
     *
     * @param id 订单明细主键
     * @return 订单明细
     */
    @Override
    public OrderDetail selectOrderDetailById(Long id) {
        return orderDetailMapper.selectOrderDetailById(id);
    }

    /**
     * 查询订单明细列表
     *
     * @param orderDetail 订单明细
     * @return 订单明细
     */
    @Override
    public List<OrderDetail> selectOrderDetailList(OrderDetail orderDetail) {
        return orderDetailMapper.selectOrderDetailList(orderDetail);
    }

    /**
     * 新增订单明细
     *
     * @param orderDetail 订单明细
     * @return 结果
     */
    @Override
    public int insertOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.insertOrderDetail(orderDetail);
    }

    /**
     * 修改订单明细
     *
     * @param orderDetail 订单明细
     * @return 结果
     */
    @Override
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateOrderDetail(orderDetail);
    }

    /**
     * 批量删除订单明细
     *
     * @param ids 需要删除的订单明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailByIds(String ids) {
        return orderDetailMapper.deleteOrderDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 批量软删除订单明细
     *
     * @param ids 需要软删除的订单明细主键
     * @return 结果
     */
    @Override
    public int softDeleteOrderDetailByIds(String ids) {
        String[] strArray = Convert.toStrArray(ids);
        int count = 0;
        for (String s : strArray) {
            OrderDetail orderDetail = orderDetailMapper.selectOrderDetailById(Long.valueOf(s));
            int i = orderDetailMapper.update(null, Wrappers.<OrderDetail>lambdaUpdate()
                    .set(OrderDetail::getIsDelete, 1L)
                    .eq(OrderDetail::getId, orderDetail.getId()));
            count = count + i;
        }
        return count;
    }

    /**
     * 删除订单明细信息
     *
     * @param id 订单明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailById(Long id) {
        return orderDetailMapper.deleteOrderDetailById(id);
    }

    @Override
    public List<OrderDetail> selectOrderDetailByOrderId(String orderId) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        return orderDetailMapper.selectOrderDetailList(orderDetail);

    }

    @Override
    public List<OrderDetail> selectOrderDetailByOrderIdAndStatus(String id, long status) {
        return orderDetailMapper.selectOrderDetailByOrderIdAndStatus(id, status);
    }

    @Override
    public OrderDetail selectOrderDetailByOrderIdAndItemId(String orderId, Long itemId) {
        return orderDetailMapper.selectOrderDetailByOrderIdAndItemId(orderId, itemId);
    }

    @Override
    public OrderDetail selectOrderDetailByOrderIdAndItemIdByReturn(String orderId, Long itemId) {
        return orderDetailMapper.selectOrderDetailByOrderIdAndItemIdByReturn(orderId, itemId);
    }
}
